<?php 
class DispatchSignaturesController extends Controller{
	
	public function actionUpdate(){
		$model = null;
		if(isset($_POST)){
			if(isset($_POST['id']) && $_POST['id']!=''){
				$model = DispatchSignatures::model()->findByPk($_POST['id']);
			}else{
				$model = new DispatchSignatures();
			}
			$model->attributes = $_POST;
			$uploaded = CUploadedFile::getInstanceByName('filename');
			if($uploaded!=null){
				$name = substr($uploaded->name,0,strlen($uploaded->name)-4);
				$filename = $name.'_'.time().'.'.$uploaded->extensionName;
				$model->filename = $filename;
				$uploaded->saveAs('documents/'.$filename);
			}

			if($model->save()){
				$this->showAjaxMessage('',true);
			}else{

				$this->showAjaxMessage('',false);
			}
		}
		$this->showAjaxMessage('Invalid parameter',true);
	}

	
	public function actionDelete($id){
		$model = DispatchSignatures::model()->findByPk($id);
		if($model!=null){
			$model->delete();
		}
	}

	public function actionList($dispatch,$sort = null,$page = 1)
	{
		$page--;
		$crit = new CDbCriteria();
		$crit->compare('dispatch',$dispatch);

		if($sort!=null){
			$sortArray = CJSON::decode($sort);
			$crit->order = $sortArray[0]['property'].' '.$sortArray[0]['direction'];
		}

		$dataProvider = new CActiveDataProvider('DispatchSignatures',array(
			'criteria'=>$crit,
			'pagination'=>array(
				'pageSize'=>20,
				'currentPage'=>$page
			)
		));

		$result = array(
			'totalCount'=>$dataProvider->getTotalItemCount()
		);

		$items = $dataProvider->getData();
		foreach($items as $index=>$item){
			$result['data'][$index] = $item->attributes;
			$result['data'][$index]['employeeName'] = Employees::getEmployeeName($item->empno);
		}

		echo json_encode($result);
	}
}